// #ifndef VUE3
// Vuex状态管理

import {
	createStore
} from 'vuex'
const store = createStore({
	state() { // 存放数据 和data类似
		return {
			title: 'Hello, Vuex',
			count: 5660
		}
	},
	mutations: { // 用来修改state和getters里面的数据
		increment(state) {
			state.count++
		}
	},
	getters: { // 相当于计算属性
		getAuthorization(state) {
			return state.Authorization
		}
	},
	actions: { // vuex中用于发起异步请求
	},
	modules: { // 拆分模块
	}
})

export default store

// #endif

// #ifdef VUE3
import config from '@/config/config.js'
import {
	api
} from '@/api/api.js' // API 链接
// pinia状态管理
import {
	defineStore
} from 'pinia'

export const useUserStore = defineStore("USER", {
	state() {
		return {
			islogin: false,
			member: {},
			msgnum: 0,
			showPrivacy: false,
			token: '',
			token_expired: 0,
			h5Weixin: {},
			systemInfo: {}, // --设备信息
			hasWebSite: false,
			CurrentSite: null,
			advBoxConfig: [], // --广告位配置项
			__key_member: 'usemall_member',
			__key_token: 'uni_id_token',
			__key_token_expired: 'uni_id_token_expired',
			__key_h5Weixin: '__h5Weixn',
			memberState: false,
			memberList: [],
			tabbarIndex: 0
		}
	},
	// 和vuex一样
	getters: {

	},
	// 和vuex一样
	actions: {
		// 登录成功
		login(res) {
			// 用户已登录
			this.islogin = true;

			this.member = res.member;
			this.token = res.user.token;
			this.token_expired = res.user.tokenExpired;

			// 存储会员 member 数据
			uni.setStorage({
				key: this.__key_member,
				data: this.member
			})

			// 存储uid token 数据
			uni.setStorage({
				key: this.__key_token,
				data: this.token
			})
			// 存储uid token_expired 数据
			uni.setStorage({
				key: this.__key_token_expired,
				data: this.token_expired
			})
		},

		// 加载 Token
		loadToken(state) {
			state.member = uni.getStorageSync(state.__key_member);
			state.token = uni.getStorageSync(state.__key_token);
			state.token_expired = uni.getStorageSync(state.__key_token_expired);

			if (state.token_expired > new Date().getTime()) {
				state.islogin = true;
			}
		},

		// 微信公众号 OAuth
		h5WeixinOAuth(state, res) {
			if (res.openid) {
				uni.setStorage({
					key: state.__key_h5Weixin,
					data: {
						code: res.code,
						openid: res.openid
					}
				});

				state.h5Weixin = res;
			}
		},

		// 微信公众号 Token
		h5WeixinToken(state, code) {
			let h5Weixin = uni.getStorageSync(state.__key_h5Weixin);
			if (h5Weixin && h5Weixin.code === code) {
				state.h5Weixin = h5Weixin;
			} else {
				state.h5Weixin = {};
				uni.removeStorageSync(state.__key_h5Weixin);
			}

			return state.h5Weixin;
		},

		// 注销 
		logout(state) {
			this.islogin = false;
			this.member = {};
			this.token = '';
			this.token_expired = 0;
			this.hasWebSite = false;
			this.CurrentSite = null;
			this.webSiteList = [];
			this.memberState = false;
			this.memberList = [];
			uni.removeStorage({
				key: this.__key_member
			})
			uni.removeStorage({
				key: '__use_web_site'
			})
			uni.removeStorage({
				key: 'use_member_invite'
			})
			uni.removeStorage({
				key: 'use_member_list'
			})
			uni.removeStorage({
				key: this.__key_token
			})
			uni.removeStorage({
				key: this.__key_token_expired
			})
		},

		// token 令牌
		token(state, token, token_expired) {
			state.token = token;
			state.token_expired = token_expired;

			// 存储uid token 数据
			uni.setStorage({
				key: state.__key_token,
				data: state.token
			})
			// 存储uid token_expired 数据
			uni.setStorage({
				key: state.__key_token_expired,
				data: state.token_expired
			})
		},
		changeLogin(res) {
			this.islogin = res;
			// 存储会员 member 数据
			uni.setStorage({
				key: this.__key_member,
				data: this.member
			})
		},
		checkAuthority(state) {
			if (!this.member.member_id) {
				return false;
			}
			return true;
		},
		// 修改 member整体 数据
		putMember(res) {
			this.member = res;
			// 存储会员 member 数据
			uni.setStorage({
				key: this.__key_member,
				data: this.member
			})
		},
		// 修改 整体member列表 数据
		putMemberList(list) {
			list.forEach((x, i) => {
				list[i].checked = false;
			})
			this.memberState = true;
			this.memberList = list;
			if (!list) {
				uni.removeStorageSync('use_member_list');
			}
			// 存储会员 member 数据
			uni.setStorage({
				key: "use_member_list",
				data: list
			})
		},
		// 修改 消息体 数据
		putMsgNum(num) {
			this.msgnum = num;
			// 存储会员 member 数据
			uni.setStorage({
				key: '__key_msgnum',
				data: num
			})
		},
		upTabbarIndex(cur) {
			this.tabbarIndex = cur;
		},
		upSystemInfo(sysInfo) {
			console.log("更新设备信息", sysInfo);
			this.systemInfo = sysInfo
		},
		setAdvConfig(advConfig) {
			this.advBoxConfig = advConfig;
		},
		getAdvConfig() {
			console.log("获取广告配置项");
			let url = config.baseTestUrl;
			let params = {
				loading: false,
				url: '/saas_N912d04/webapi/eidData.ashx?oper=LTTG_Multifunction',
				method: 'get',
				data: {
					storedPro: 'P_SaaS_GetAdvConfig',
					paraNum: 2,
					resultSetNum: 1,
					para1: config.wxMpKey,
					para2: ''
				}
			}
			api.getData(url, params).then((res) => {
				try {
					console.log("获取广告配置项", res);
					let _list = [];
					res.data1.forEach((x, index) => {
						_list.push({
							type: x.pages,
							time: x.time,
							end: x.cycle_end,
							start: x.cycle_start,
							fileid: x.fileid,
							form: x.show_form,
							frequency: x.frequency
						})
					})
					this.advBoxConfig = {
						list: _list,
						date: new Date().getTime()
					};
				} catch (e) {
					console.log(e)
				}
			})
		}
	}
})
// #endif